package com.neusoft.shopping.manager.controller;

import com.neusoft.shopping.manager.entity.TypeEntity;
import com.neusoft.shopping.manager.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class TypeController {
    @Autowired
    private TypeService service;

    @RequestMapping("/type_show")
    public ModelAndView gettype(){
        ModelAndView modelAndView=new ModelAndView();
      List<TypeEntity> types= service.qureyAll();
        modelAndView.addObject("types",types);
        modelAndView.setViewName("type_show");
        return modelAndView;
    }

    @RequestMapping("/typeModify/{method}/{typeno}")
    public ModelAndView typeModify(@PathVariable("method") String method, @PathVariable("typeno") Integer typeno){
        ModelAndView modelAndView=new ModelAndView();
        if(method.equals("update")){
            TypeEntity type=service.qureyByTypeno(typeno);
            modelAndView.addObject("type",type);
            modelAndView.setViewName("type_modify");
            return modelAndView;
        }
        else if (method.equals("add")){
            modelAndView.setViewName("type_modify");
            return modelAndView;
        }else {
            service.del_type(typeno);
            return this.gettype();
        }
    }
    @RequestMapping("/typeModifyConserve")
    public ModelAndView  typeModifyConserve(@RequestParam("method") String method ,TypeEntity type){
        if(method.equals("add")){
            service.add_type(type);
        }
        if(method.equals("update")){
            service.update_type(type);
        }
        return this.gettype();
    }
}
